package br.ufrj.ppgi.greco.controller;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.ufrj.ppgi.greco.model.dao.DAOProperty;
import br.ufrj.ppgi.greco.model.dao.DAOResource;
import br.ufrj.ppgi.greco.model.dao.DAOStaticDataProperty;
import br.ufrj.ppgi.greco.model.entity.EntityResource;
import br.ufrj.ppgi.greco.model.entity.EntityStaticDataProperty;

@Resource
public class StaticDataPropertyController
{
	private DAOResource dr;
	private DAOProperty dp;
	private DAOStaticDataProperty ds;
	private Result result;
	
	public StaticDataPropertyController(DAOResource dr, DAOStaticDataProperty ds, Result result, DAOProperty dp)
	{
		this.dr = dr;
		this.dp = dp;
		this.ds = ds;
		this.result = result;
	}
	
	
	@Get
	@Path("/config/resources/{id}/staticDataProperties/new")
	public void newStaticDataProperty(int id)
	{
		this.result.include("id",id);
		this.result.include("properties",this.dp.getAllPropertiesJson());
	}
	
	@Post
	@Path("/config/resources/{id}/staticDataProperties/create")
	public void createStaticDataProperty(EntityStaticDataProperty sdp, int id)
	{
		EntityResource er = this.dr.getByID(id);
		sdp.setResource(er);
		
		this.ds.insert(sdp);
		er.getStaticDataProperties().add(sdp);
		this.dr.update(er);
		this.result.redirectTo(ResourceController.class).listResource(id);
	}
	
	@Get
	@Path("/config/resources/{id}/staticDataProperties/{idStaticDP}")
	public void listStaticDataProperty(int id, int idStaticDP)
	{
		this.result.include("static",this.ds.getByID(idStaticDP));
		this.result.include("id",id);
		this.result.include("properties",this.dp.getAllPropertiesJson());
	}
	
	@Post
	@Path("/config/resources/{id}/staticDataProperties/update")
	public void updateStaticDataProperty(EntityStaticDataProperty sdp,int id)
	{
		sdp.setResource(this.dr.getByID(id));
		this.ds.update(sdp);
		this.result.redirectTo(ResourceController.class).listResource(id);
	}
	
	@Get
	@Path("/config/resources/{resourceId}/staticDataProperties/{idStatic}/delete")
	public void deleteStaticDataProperty(int resourceId, int idStatic)
	{
		this.ds.deleteByID(idStatic);
		this.result.redirectTo(ResourceController.class).listResource(resourceId);
	}

	
	
}
